package com.itheima.juc23.question5;

import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.Callable;

/**
 * @author li
 * 抽奖
 */
public class MyCallable implements Callable<Integer> {
    // 定义共享数据:{10,50,2,60,100,200,500,800,2,80,300,700}，定义集合最好
    ArrayList<Integer> list;

    public MyCallable(ArrayList<Integer> list) {
        this.list = list;
    }

    @Override
    public Integer call() {
        ArrayList<Integer> boxList = new ArrayList<>();
        while (true) {
            synchronized (MyCallable.class) {
                if (list.isEmpty()) {
                    System.out.println(Thread.currentThread().getName() + boxList + "，最大值：" + Collections.max(boxList) + "，和：" + boxList.stream().mapToInt(Integer::intValue).sum());
                    break;
                } else {
                    // 继续抽奖
                    Collections.shuffle(list);
                    Integer prize = list.remove(0);
                    boxList.add(prize);
                }
            }
        }
        if (boxList.isEmpty()){
            return null;
        }
        return Collections.max(boxList);
    }
}
